﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using PH.Model.Accounts;
using PH.Logic.Accounts;
namespace PH.Web.Management.Areas.Accounts.Controllers
{
    [RouteArea("Accounts")]
    [RoutePrefix("User")]
    public class UserController : Controller
    {
        [Route("")]
        [Authorize]
        public ActionResult Index()
        {
            var user=PH.Web.Global.WebContext.CurrentUser;
            if (user.Roles.Any(p => p.RoleName == "Bureau"))
                ViewBag.Bureau = PH.Logic.Systems.BureauLogic.GetModel(user.AssociatedBureauID.Value);
            else if (user.Roles.Any(p => p.RoleName == "SchoolAdmin"))
                ViewBag.School = PH.Logic.Schools.SchoolLogic.GetSchool(user.AssociatedSchoolID.Value);
            return View(PH.Web.Global.WebContext.CurrentUser);
        }

        [Route("Login")]
        public ActionResult Login()
        {
            return View();
        }

        [Route("Logout")]
        public ActionResult Logout()
        {
            PH.Web.Global.WebContext.LogOut();
            return View("Login");
        }

        [Route("Password")]
        [Authorize]
        public ActionResult Password()
        {
            return View();
        }

        [Route("ForgetPassword")]
        public ActionResult ForgetPassword()
        {
            return View();
        }

        [Route("ResetPassword/{guid:guid}")]
        public ActionResult ResetPassword(Guid guid)
        {
            var user = UserLogic.GetUser(guid);
            if (user != null && user.ResetPasswordExpired != null && user.ResetPasswordExpired.Value.CompareTo(DateTime.Now) >= 0)
                return View(user);
            else
                throw new HttpException(404, "此页面已过期");
        }

        [Route("Email/{guid:guid}")]
        [Authorize(Roles = "Bureau")]
        public ActionResult Email(Guid guid)
        {
            var model = UserLogic.GetUser(guid);
            return PartialView(model);
        }
    }
}
